# encoding:utf-8


from flask import Flask,session,g

from models import *

import config

from ests import db


app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)

#注册蓝图，为创建后台打下基础
from home import home as home_bp
app.register_blueprint(home_bp)



@app.before_request
def my_before_request():
    user_id = session.get('user_id')
    if user_id:
        user = User.query.filter(User.id == user_id).first()
        if user:
            g.user = user


@app.context_processor
def my_context_processor():
    if hasattr(g,'user'):
            return {'user': g.user}
    else:
        return {}  # 被钩子函数装饰的函数即使是空的也要返回一个空字典


if __name__ == '__main__':
    app.run(debug=True)
